package com.clean.cleantotal.compores.empty

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.clean.cleantotal.R
import com.clean.cleantotal.base.BaseActivity
import com.clean.cleantotal.bean.FunType
import com.clean.cleantotal.bean.JunkBean
import com.clean.cleantotal.compores.delete.DeleteActivity
import com.clean.cleantotal.databinding.ActivityEmptyBinding
import com.clean.cleantotal.utils.ScanJunkUtils
import com.clean.cleantotal.utils.Utils
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup

class EmptyActivity : BaseActivity<ActivityEmptyBinding>() {
    override val viewBinding: ActivityEmptyBinding
        get() = ActivityEmptyBinding.inflate(layoutInflater)

    private var emptyList = ArrayList<JunkBean>()
    private var selectList = HashSet<JunkBean>()
    private var isAllSelect = false

    private val scanJunkUtils: ScanJunkUtils = ScanJunkUtils()

    override fun initView() {
        mBinding.back.setOnClickListener { onBackPressed() }
        Utils.showScanDialog(this, FunType.EMPTY, mBinding.loadingLayout.scanLayout, {
            animal = it
        }) {

        }
        mBinding.recyclerView.linear().setup {
            addType<JunkBean> { R.layout.item_empty_layout }
            onClick(R.id.emptyLayout) {
                val model = getModel<JunkBean>()
                model.isChecked = !model.isChecked
                if (model.isChecked) {
                    selectList.add(model)
                } else {
                    selectList.remove(model)
                }
                isAllSelect = selectList.size == emptyList.size
                mBinding.ivAll.setImageResource(if (isAllSelect) R.mipmap.green_clean_hospital else R.mipmap.green_clean_careful)
                mBinding.recyclerView.bindingAdapter.notifyItemChanged(position)
                mBinding.tvClean.text = "Clean (${selectList.size})"
            }
        }.models = emptyList

        animal?.duration = 3000
        scanJunkUtils.scanEmptyFiles(lifecycleScope) {
            emptyList.clear()
            emptyList.addAll(it)
            val totalSize = emptyList.size
            mBinding.includedLayout.yellowYuan.isVisible = true
            mBinding.includedLayout.ivFinish.isVisible = false
            mBinding.includedLayout.tvSize.text = "$totalSize"
            mBinding.includedLayout.tvTitle.text = "Free Space"
            mBinding.includedLayout.tvContent.text = "$totalSize empty files found"
            mBinding.recyclerView.bindingAdapter.notifyDataSetChanged()
            if (emptyList.isEmpty()) {
                mBinding.stateLayout.showEmpty()
            } else {
                mBinding.stateLayout.showContent()
            }
        }

        mBinding.allLayout.setOnClickListener {
            isAllSelect = !isAllSelect
            emptyList.forEach {
                it.isChecked = isAllSelect
                if (it.isChecked) {
                    selectList.add(it)
                } else {
                    selectList.remove(it)
                }
            }
            mBinding.recyclerView.bindingAdapter.notifyDataSetChanged()
            mBinding.ivAll.setImageResource(if (isAllSelect) R.mipmap.green_clean_hospital else R.mipmap.green_clean_careful)
            mBinding.tvClean.text = "Clean (${selectList.size})"
        }

        mBinding.tvClean.setOnClickListener {
            Utils.selectEmptyList.clear()
            Utils.selectEmptyList.addAll(selectList)
            openActivity(DeleteActivity::class.java, Bundle().apply {
                putSerializable("funType", FunType.EMPTY)
            })
            finish()
        }
    }

}